package cn.wolfcode.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.wolfcode.domain.Appointment;
import cn.wolfcode.domain.Consumption;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.enums.ConsumptionEnums;
import cn.wolfcode.mapper.AppointmentMapper;
import cn.wolfcode.mapper.ConsumptionMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IConsumptionService;
import cn.wolfcode.util.UtilContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Service
public class ConsumptionServiceImpl implements IConsumptionService {

    @Autowired
    private ConsumptionMapper consumptionMapper;
    @Autowired
    private AppointmentMapper appointmentMapper;

    @Override
    public void save(Consumption consumption) {
        consumptionMapper.insert(consumption);
    }

    @Override
    public void delete(Long id) {
        consumptionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Consumption consumption) {
        consumptionMapper.updateByPrimaryKey(consumption);
    }

    @Override
    public Consumption get(Long id) {
        Consumption consumption = consumptionMapper.selectByPrimaryKey(id);
        return consumption;
    }

    @Override
    public List<Consumption> listAll() {
        return consumptionMapper.selectAll();
    }

    @Override
    public PageInfo<Consumption> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize()); //对下一句sql进行自动分页
        List<Consumption> consumptions = consumptionMapper.selectForList(qo); //里面不需要自己写limit
        return new PageInfo<Consumption>(consumptions);
    }

    @Override
    public Consumption save(String ano) {
        //获取当前时间
        Date date = new Date();
        //根据预约单流水号在预约单查找预约单
        Appointment appointment = appointmentMapper.selectByAno(ano);
        //把预约单号改成待结算
        appointmentMapper.updateStatus(appointment.getId(), ConsumptionEnums.CONSUME.getValue());
        //创建一个新的结算单对象
        Consumption consumption = new Consumption();
        //设置客户名称
        consumption.setCustomerName(appointment.getContactName());
        //设置客户电话
        consumption.setCustomerTel(appointment.getContactTel());
        //设置消费门店
        consumption.setBusiness(appointment.getBusiness());
        //设置进店时间
        consumption.setCheckinTime(date);
        //设置结算单流水号
        //获取当前时间
        String format = DateUtil.format(date, "yyyyMMddHHmmss");
        //获取随机数
        String numbers = RandomUtil.randomNumbers(5);
        //设置预约单号--->时间+5位随机数
        consumption.setCno(format+numbers);
        //关联预约单流水号
        consumption.setAppointmentAno(ano);
        //创建时间
        consumption.setCreateTime(date);
        //把结算单对象给添加到结算单里
        consumptionMapper.insert(consumption);

        return consumption;
    }
}
